<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>
        X-admin v1.0
    </title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <link rel="stylesheet" href="/fcdb/css/x-admin.css" media="all">
    <%--<link rel="stylesheet" href="/plugin/zTree/css/demo.css" type="text/css">--%>
    <link rel="stylesheet" href="/fcdb/plugin/zTree/css/zTreeStyle/zTreeStyle.css?v=1" type="text/css">
</head>

<body>
<div class="x-body">
    <form class="layui-form" method="post" action="">

        <div class="layui-form-item">
            <label for="roleName" class="layui-form-label">
                <span class="x-red">*</span>角色名
            </label>
            <div class="layui-input-inline">
                <input type="text" id="roleName" name="roleName"  required="" lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="menuIds" class="layui-form-label">
                <span class="x-red">*</span>拥有菜单
            </label>
            <div id="menuIds" class="layui-input-inline">
                <ul id="treeDemo" class="ztree"></ul>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="add">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/fcdb/lib/layui/layui.js" charset="utf-8">
</script>
<script src="/fcdb/js/x-layui.js" charset="utf-8"></script>
<script src="/fcdb/js/jquery-1.12.3.min.js" charset="utf-8"></script>
<!-- tree -->
<script type="text/javascript"
        src="/fcdb/plugin/zTree/js/jquery.ztree.core.js?v=1"></script>
<script type="text/javascript"
        src="/fcdb/plugin/zTree/js/jquery.ztree.excheck.js?v=1"></script>
<script>
    layui.use(['form','layer'], function(){
        $ = layui.jquery;
        var form = layui.form()
                ,layer = layui.layer;

        //监听提交
        form.on('submit(add)', function(data){
            getNodeIds();
            $.ajax({
                url:"/fcdb/role/saveOrUpdate.do",
                dataType:"json",
                data:{
                    roleName:$("#roleName").val(),
                    nodeIds:tempNodeIds,
                },
                type:"POST",
                async:false,
                success:function (res) {
                    if(res.msg=="success"){
                        layer.alert("编辑成功", {icon: 6},function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.location.reload();
                            parent.layer.close(index);
                        });
                    }else{
                        layer.msg("增加失败", {icon: 1});
                    }
                },
                error:function (XMLHttpRequest) {
                    layer.msg("系统错误", {icon: 1});
                }
            })

            return false;
        });


    });

    $(document).ready(function() {
        var treeNodes=${list};
            $.fn.zTree.init($("#treeDemo"), setting, treeNodes);
            setCheck();
            $("#py").bind("change", setCheck);
            $("#sy").bind("change", setCheck);
            $("#pn").bind("change", setCheck);
            $("#sn").bind("change", setCheck);

    });
    var tempNodeIds;
    function getNodeIds() {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo");
        var checkedNodes = zTree.getCheckedNodes(true);
        var nodeIds = new Array();
        for (var i = 0; i < checkedNodes.length; i++) {
            var checkedNode = checkedNodes[i];
            nodeIds.push(checkedNode.id);
        }
        tempNodeIds = nodeIds.join(',');
    }

    // tree
    var setting = {
        check : {
            enable : true,
            chkStyle : "checkbox",
            chkboxType : {
                "Y" : "ps",
                "N" : "ps"
            }
        },
        data : {
            simpleData : {
                enable : true
            }
        }
    };

    var code;
    function showCode(str) {
        if (!code)
            code = $("#code");
        code.empty();
        code.append("<li>" + str + "</li>");
    }
    function setCheck() {
        var zTree = $.fn.zTree.getZTreeObj("treeDemo"), py = $("#py").attr(
                "checked") ? "p" : "s", sy = $("#sy").attr("checked") ? "s" : "p", pn = $(
                "#pn").attr("checked") ? "p" : "s", sn = $("#sn").attr("checked") ? "s"
                : "p", type = {
            "Y" : py + sy,
            "N" : pn + sn
        };
        zTree.setting.check.chkboxType = type;
        showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "'
                + type.N + '" };');
    }
</script>
</body>

</html>
